package homelesspartners.client.widgets;

import homelesspartners.client.model.ThankYou;
import homelesspartners.client.model.ThankYouPages;

import java.util.ArrayList;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ThankYouNotesWidget extends Composite implements ClickListener {

	private VerticalPanel verticalPanel = new VerticalPanel();
	private VerticalPanel list = new VerticalPanel();

	private HTML headingText =
			new HTML(
					"&nbsp;<div id='top'></div><b>Thank You Notes</b><br /><br />The following thank you notes were provided by the individuals who recieved Christmas gifts through the program this year.<br /><br /><br />");

	private PushButton previous = new PushButton("Previous " + ThankYouPages.PAGE_SIZE, this);
	private PushButton next = new PushButton("Next " + ThankYouPages.PAGE_SIZE, this);
	private HTML rule = new HTML("<hr />");
	private HTML thankyouCount = new HTML("Thank-Yous #");
	private HTML noMoreThankYous =
			new HTML(
					"There are no more thank you notes available yet.  We will be adding more after the gifts have been distributed on Christmas Day.  Please check back later.  Thanks, and Merry Christmas!<br /><br />");
	private HorizontalPanel buttonPanel = new HorizontalPanel();

	private static ThankYouNotesWidget instance = new ThankYouNotesWidget();

	public static ThankYouNotesWidget getInstance() {
		return instance;
	}

	public ThankYouNotesWidget() {
		verticalPanel.add(headingText);

		previous.setVisible(false);
		next.setVisible(false);
		thankyouCount.setVisible(false);

		Label spacer1 = new Label(" ");
		HTML spacer2 = new HTML("<br /><br />");

		thankyouCount.setWidth("500px");
		previous.setWidth("100px");
		next.setWidth("100px");
		spacer1.setWidth("10px");
		spacer2.setWidth("10px");

		buttonPanel.add(thankyouCount);
		buttonPanel.add(previous);
		buttonPanel.add(spacer1);
		buttonPanel.add(next);
		buttonPanel.add(spacer2);

		verticalPanel.add(list);

		verticalPanel.add(noMoreThankYous);
		noMoreThankYous.setVisible(false);

		verticalPanel.add(rule);
		verticalPanel.add(buttonPanel);

		initWidget(verticalPanel);
	}

	public void refresh() {

		ArrayList thankyous = ThankYouPages.getInstance().getPage();
		System.out.println(thankyous);
		if (thankyous == null) {
			noMoreThankYous.setVisible(true);
			return;
		} else if (thankyous.size() == 0) {
			noMoreThankYous.setVisible(true);
		} else {
			noMoreThankYous.setVisible(false);
		}

		list.clear();
		for (int x = 0; x < thankyous.size(); x++) {
			ThankYou thanks = (ThankYou) thankyous.get(x);
			list.add(new HTML("<b>" + thanks.getTitle() + " " + "</b><br />"));
			list.add(new HTML(thanks.getThankyou() + "<br /><br />"));
		}

		thankyouCount.setVisible(false);
		previous.setVisible(false);
		next.setVisible(false);

		if (ThankYouPages.getInstance().hasMore()) {
			next.setVisible(true);
			thankyouCount.setVisible(true);
		}

		if (ThankYouPages.getInstance().hasPrevious()) {
			previous.setVisible(true);
			thankyouCount.setVisible(true);
		}

		if (previous.isVisible() && next.isVisible()) {
			thankyouCount.setWidth("300px");
		} else {
			thankyouCount.setWidth("400px");
		}

		if (thankyouCount.isVisible()) {
			int offsetStart = ThankYouPages.getInstance().getCurrentPageStartIndex();
			int offsetEnd = ThankYouPages.getInstance().getCurrentPageEndIndex();
			String label = thankyouCount.getText();
			label = label.substring(0, label.indexOf('#') + 1);
			label = label + offsetStart + " - " + offsetEnd;
			thankyouCount.setHTML(label);

		}

	}

	public void onClick(Widget sender) {

		DOM.scrollIntoView(DOM.getElementById("top"));

		if (sender == next) {
			ThankYouPages.getInstance().nextPage();
			refresh();
		} else if (sender == previous) {
			ThankYouPages.getInstance().previousPage();
			refresh();
		}

	}

}
